import sys

from pygame_test.Maze import Maze
from pygame_test.Sector import Sector
import pygame


# 控制方向和角度
# 上下左右控制方向, 同时改变开口朝向
# 只能沿格子中心线移动
class EatBean:
    def __init__(self):
        winx = 300
        winy = 300
        pygame.init()
        screen = pygame.display.set_mode((winx, winy))
        r = 20
        maze_x = 20  # 迷宫左上角坐标
        maze_y = 20
        x = maze_x + r  # 吃豆人起止位置中心点
        y = maze_y + r
        b = mouse1 = 315  # 上嘴张开角度
        e = mouse2 = 225  # 下嘴张开角度
        mouse0 = 270  # 嘴闭合的角度
        open_speed = 3
        speed = 2
        speedx = speed
        speedy = speed
        mouse_open = False

        arr = [[1, 0, 0, 0, 1, 1]
            , [1, 1, 1, 1, 1, 0]
            , [0, 1, 0, 0, 0, 0]
            , [0, 1, 0, 0, 0, 0]
            , [0, 1, 0, 0, 0, 0]
            , [0, 1, 1, 0, 0, 0]]
        arr_row_num = len(arr)
        arr_col_num = len(arr[0])
        maze = Maze(maze_x, maze_y, r * 2, arr)
        print(maze.get_point(0, 0))

        rects = []  # beans列表
        j = 0
        move = 'still'
        fclock = pygame.time.Clock()
        while True:

            # 当前吃豆人所处格子的索引i,j 和中心点坐标
            i, j = maze.get_indexes(x, y)
            if i is None:
                # 吃豆人中心点在横线上
                y += speedy  # 跨过这条线
                i, j = maze.get_indexes(x, y)
            elif j is None:
                # 吃豆人中心点在竖线上
                x += speedx  # 跨过这条线
                i, j = maze.get_indexes(x, y)

            center_x = maze.get_point(i, j)[4][0]
            center_y = maze.get_point(i, j)[4][1]

            sector = Sector(x, y, r, (255, 255, 255), b, e)
            # print(sector.x, center_x)
            # print(sector.y, center_y)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()
                    elif event.key == pygame.K_UP:
                        # if abs(sector.y - center_y)<=speed:
                        move = 'up'
                        x = center_x # 强制走中心线
                        b = mouse1 = 135  # 上嘴张开角度
                        e = mouse2 = 45  # 下嘴张开角度
                        mouse0 = 90  # 嘴闭合的角度
                    elif event.key == pygame.K_DOWN:
                        # if abs(sector.y - center_y)<=speed:
                        move = 'down'
                        x = center_x # 强制走中心线
                        b = mouse1 = 315  # 上嘴张开角度
                        e = mouse2 = 225  # 下嘴张开角度
                        mouse0 = 270  # 嘴闭合的角度
                    elif event.key == pygame.K_LEFT:
                        # if abs(sector.x - center_x)<=speed:
                        move = 'left'
                        y = center_y # 强制走中心线
                        b = mouse1 = 225  # 上嘴张开角度
                        e = mouse2 = 135  # 下嘴张开角度
                        mouse0 = 180  # 嘴闭合的角度
                    elif event.key == pygame.K_RIGHT:
                        # if abs(sector.x - center_x)<=speed:
                        move = 'right'
                        y = center_y # 强制走中心线
                        b = mouse1 = 45  # 上嘴张开角度
                        e = mouse2 = 315  # 下嘴张开角度
                        mouse0 = 0  # 嘴闭合的角度
            # 遇到maze边缘停止
            if x > maze.get_point(arr_row_num - 1, arr_col_num - 1)[4][0] and move == 'right':
                move = 'still1'
            elif x < maze.get_point(0, 0)[4][0] and move == 'left':
                move = 'still2'
            elif y > maze.get_point(arr_row_num - 1, arr_col_num - 1)[4][1] and move == 'down':
                move = 'still3'
            elif y < maze.get_point(0, 0)[4][1] and move == 'up':
                move = 'still4'

            # 下一格是0, 并且吃豆人中心点在当前格子的中心点处, 停止
            # elif i is not None or j is not None:
            if maze.judge_neighbor(i, j)[0] == 0 and move == 'up' and y < maze.get_point(i, j)[4][1]:
                print(maze.judge_neighbor(i, j)[1], y, maze.get_point(i, j)[4][1], i, j)
                move = 'still5'
            elif maze.judge_neighbor(i, j)[1] == 0 and move == 'down' and y > maze.get_point(i, j)[4][1]:
                print(maze.judge_neighbor(i, j)[1], y, maze.get_point(i, j)[4][1], i, j)
                move = 'still6'
            elif maze.judge_neighbor(i, j)[2] == 0 and move == 'left' and x < maze.get_point(i, j)[4][0]:
                move = 'still7'
            elif maze.judge_neighbor(i, j)[3] == 0 and move == 'right' and x > maze.get_point(i, j)[4][0]:
                move = 'still8'
            # print(move)

            if move == 'up':
                speedx = 0
                speedy = -speed
            elif move == 'down':
                speedx = 0
                speedy = speed
            elif move == 'left':
                speedx = -speed
                speedy = 0
            elif move == 'right':
                speedx = speed
                speedy = 0
            # elif move == 'still':
            elif move.startswith('still'):
                speedx = 0
                speedy = 0
            # 吃豆人移动
            x += speedx
            y += speedy

            if b >= mouse1:
                mouse_open = False
            elif b <= mouse0:
                mouse_open = True
            if mouse_open:
                b += open_speed
                e -= open_speed
            else:
                b -= open_speed
                e += open_speed

            screen.fill((0, 0, 0))
            sector.draw(screen)
            maze.draw(screen)
            # print(sector.x,sector.y)

            pygame.display.update()
            fclock.tick(60)


if __name__ == '__main__':
    eat_bean = EatBean()
